<script lang="ts" setup>
import { ref, onActivated } from "vue";
import router from "@/router";
import applyForm from "./components/applyForm.vue";
import PageWrapper from "@/components/Wrapper";
import detailTable from "./components/detailTable.vue";
import operation from "./components/operation.vue";
import {
  getWarehouseInventoryAdjustDetail,
  setWarehouseInventoryAdjustUpdate
} from "@/api/warehouseInventoryAdjust";
import { useMessage } from "@/hooks/useMessage";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
import { useRoute } from "vue-router";

const { createNotification } = useMessage();
const multiTagsStore = useMultiTagsStoreHook();
const route = useRoute();
defineOptions({
  name: "warehouseInventoryAdjustEditDetail"
});
const tableData = ref([]);

const tableHeight = ref(0);
const pageWrapperRef = ref();
const applyFormRef = ref(null);
const tableRef = ref(null);

onActivated(() => {
  const { id } = router.currentRoute.value.query;
  initData(id);
  pageWrapperRef.value.initData();
});

// 获取数据
const initData = (id: any) => {
  getWarehouseInventoryAdjustDetail({ id: Number(id) }).then(rsp => {
    applyFormRef.value.setForm(rsp.data);
    tableData.value = rsp.data.itemList;
  });
};

// 提交
const submitApply = () => {
  const { id } = router.currentRoute.value.query;
  setWarehouseInventoryAdjustUpdate(
    Object.assign({ id }, applyFormRef.value.getForm())
  ).then(rsp => {
    if (rsp.code === 0) {
      createNotification({
        title: "温馨提示",
        message: rsp.msg,
        type: "success"
      });
      multiTagsStore.handleTags("splice", route.path);
      router.back();
    }
  });
};
</script>
<template>
  <div class="main_box">
    <PageWrapper
      ref="pageWrapperRef"
      v-model:height="tableHeight"
      :isShowBottom="false"
      :isShowLine="false"
    >
      <template #header>
        <applyForm ref="applyFormRef" />
      </template>
      <template #content>
        <div class="table_box">
          <operation />
          <detailTable
            ref="tableRef"
            :list="tableData"
            :tableHeight="tableHeight"
          />
        </div>
      </template>
      <template #footer>
        <section
          class="flex flex-row justify-between pb-[16px] pt-[16px] pr-[24px] pl-[24px] bg-white rounded-t-[8px]"
        >
          <div class="flex flex-row" />

          <el-button type="primary" @click="submitApply">保存</el-button>
        </section>
      </template>
    </PageWrapper>
  </div>
</template>
<style lang="scss" scoped></style>
